package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
/**
 * slf4j注解用于开启日志
 * 处理查询和新增请求
 */
public class PaymentController {
    @Autowired
    PaymentService paymentService;
    @Value("${server.port}")
    private String serverPort;
    @PostMapping(value = "/payment/create")
    public CommonResult create(@RequestBody Payment payment) {
        //调用插入方法 返回值大于0 成功 否则失败
        int result = paymentService.create(payment);
        //开启日志
        log.info("插入结果：" + result);
        if (result > 0) {
            return new CommonResult(200, "数据插入成功serverPort:"+serverPort, result);
        } else {
            return new CommonResult(444, "数据插入失败", null);
        }

    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable(value = "id") Long id) {
        //调用插入方法 返回值大于0 成功 否则失败
        Payment payment = paymentService.getPaymentById(id);
        //开启日志
        log.info("查询结果：" + payment);

        if (payment != null) {
            return new CommonResult(200, "数据查询成功serverPort:"+serverPort, payment);
        } else {
            return new CommonResult(444, "没有对应记录,id:" + id, null);
        }

    }

}
